package com.man;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.std.DB;
import com.std.Link;

public class LinkCtrl {
	private DB db;
	public LinkCtrl(){
		db=new DB();
	}
	public void insertItem(String url,String site) {
		String sql="insert into link_t values(link_s.nextval,?,?)";
		Object []params={url,site};
		db.execute(sql, params);
		
	}
	public List<Link> getItems(){
		List<Link> list=new ArrayList<Link>();
		String sql="select * from link_t";
		db.execute(sql, null);
		ResultSet rs=db.getResultset();
		try {
			while(rs.next()){
				Link link=new Link();
				link.setLinkId(rs.getInt("link_id"));
				link.setSite(rs.getString("site"));
				link.setUrl(rs.getString("url"));
				list.add(link);
			}		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		
		return list;
	}
	public void deleteItem(int linkId) {
		String sql="delete from link_t ";
		Object []params=new Object[1];
		int length=0;
		if(linkId!=0){
			sql=sql+"where link_id=?";
			params[length++]=linkId;
		}
		db.execute(sql, params);
		
	}
	public void updateItem(int linkId,String url,String site) {
		String sql="update link_t set url=?,site=? where link_id=?";
		Object []params={url,site,linkId};
		db.execute(sql, params);
		
	}
	public void close() {
		db.close();
	}
}
